package com.homework.module6part3.consumer;

import com.homework.dao.domain.Goods;
import com.homework.dao.domain.Order;
import com.homework.dao.domain.OrderMessage;
import com.homework.module6part3.service.GoodsService;
import com.homework.module6part3.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Base64;

/**
 * 消息监听器
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "seckill_orders", consumerGroup = "springboot-mq-consumer-1")
public class RocketMqConsumer implements RocketMQListener<String>  {
    @Autowired
    protected OrderService orderService;

    @Autowired
    protected GoodsService goodsService;

    @Override
    public void onMessage(String message)  {
        System.out.println("收到消息：" + message);
        int orderId = Integer.parseInt(message);
        //判断订单是否支付
        Order order = orderService.getOrder(orderId);
        if(order == null) {
            return;
        }
        if(order.getPayStatus() == 0) {
            //修改订单状态为支付超时
            orderService.updateOrderPayStatus(orderId, 2);
            //增加库存
            goodsService.backStock(order.getGoodsId());
        }
    }

    protected void messageProcess(OrderMessage message) {

    }
}